<template>
  <ele-page>
    <!-- 搜索表单 -->
    <info-search @search="reload"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="fileId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="cmsInfoTable"
      >
        <template #toolbar>
          <el-button
            type="danger"
            class="ele-btn-icon"
            :icon="DeleteOutlined"
            @click="removeBatch()"
          >
            删除
          </el-button>
<!--          <el-button class="ele-btn-icon" :icon="DownloadOutlined" @click="exportData">-->
<!--            导出-->
<!--          </el-button>-->
        </template>
        <template #action="{ row }">
          <el-link type="danger" :underline="false" @click="removeBatch(row)">
            删除
          </el-link>
        </template>
        <template #pic="{ row }">
          <el-image
            style="width: 120px;height: 60px;"
            :src="row.pic"
            lazy
            :preview-src-list="[row.pic]" :preview-teleported="true">
          </el-image>
        </template>
      </ele-pro-table>
    </ele-card>
  </ele-page>
</template>

<script setup>
import {ref} from 'vue';
import {DeleteOutlined, DownloadOutlined, PlusOutlined} from '@/components/icons';
import {ElMessageBox} from 'element-plus/es';
import {EleMessage} from 'ele-admin-plus/es';
import InfoSearch from './components/info-search.vue';
import {exportInfos, pageInfos, removeInfos} from '@/api/cms/fileInfo';


/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    type: 'selection',
    columnKey: 'selection',
    width: 50,
    align: 'center',
    fixed: 'left'
  },
  {
    prop: 'fileOriginName',
    label: '图片预览',
    align: 'center',
    minWidth: 120,
    slot: 'pic'
  },
  {
    prop: 'fileOriginName',
    label: '文件名称',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'fileSuffix',
    label: '文件类型',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'fileSizeInfo',
    label: '文件大小',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'fileObjectName',
    label: '存储文件名称',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'filePath',
    label: '存储路径',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'createBy',
    label: '创建者',
    align: 'center'
  },
  {
    prop: 'createTime',
    label: '创建时间',
    align: 'center'
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 180,
    align: 'center',
    slot: 'action'
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/** 表格数据源 */
const datasource = ({pages, where}) => {
  return pageInfos({...where, ...pages});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};


/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error('请至少选择一条数据');
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除文件名称为"${rows.map((d) => d.fileOriginName).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading('请求中..');
      removeInfos(rows.map((d) => d.fileId))
        .then(() => {
          loading.close();
          EleMessage.success('删除成功');
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error(e.message);
        });
    })
    .catch(() => {
    });
};

/** 导出数据 */
// const exportData = () => {
//   const loading = EleMessage.loading('请求中..');
//   tableRef.value?.fetch?.(({where}) => {
//     exportInfos(where)
//       .then(() => {
//         loading.close();
//       })
//       .catch((e) => {
//         loading.close();
//         EleMessage.error(e.message);
//       });
//   });
// };
</script>
